/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-18
*/
package com.rzico.order.entity;

import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Member;
import com.rzico.order.enumx.OrderPaymentStatusEnum;
import com.rzico.order.enumx.OrderShippingStatusEnum;
import com.rzico.order.enumx.OrderStatusEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.*;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   退货申请单数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_askfor")
public class Askfor{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "申请单号" )
    protected String sn;

    @ApiModelProperty(value = "所在地" )
    protected Integer areaId;

    @ApiModelProperty(value = "所属企业")
    protected Long companyId;

    @ApiModelProperty(value = "收货地址" )
    protected String address;

    @ApiModelProperty(value = "所在地" )
    protected String areaName;

    @ApiModelProperty(value = "收货人" )
    protected String consignee;

    @ApiModelProperty(value = "联系电话" )
    protected String phone;

    @ApiModelProperty(value = "退货说明" )
    protected String memo;

    @ApiModelProperty(value = "退货理由" )
    protected String reason;

    @ApiModelProperty(value = "申请状态(0:未确认,1:已同意,2:已寄回,3.待退款,4.已完成,5.已关闭)" )
    protected Integer status;

    @ApiModelProperty(value = "配送方式(0.普通快递,1.到店核销,2.同城物流)")
    protected Integer shippingMethod;

    @ApiModelProperty(value = "订货方式(0.线上订单，1.人工报单，2.门店销售)")
    protected Integer orderMethod;

    @ApiModelProperty(value = "是否已分配库存", hidden = true)
    protected Boolean isAllocatedStock;

    @ApiModelProperty(value = "买方会员" )
    protected Long memberId;

    @ApiModelProperty(value = "来源订单" )
    protected Long orderId;

    @ApiModelProperty(value = "所属门店" )
    protected Long shopId;

    @ApiModelProperty(value = "来源订单号")
    protected String orderSn;

    @ApiModelProperty(value = "所属企业" )
    protected Long enterpriseId;

    @ApiModelProperty(value = "退货时间" )
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "到期时间" )
    protected java.util.Date expire;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected java.math.BigDecimal subQuantity;

    @ApiModelProperty(value = "运费(含楼层费)")
    protected java.math.BigDecimal freight;

    @ApiModelProperty(value = "楼层费")
    protected java.math.BigDecimal levelFreight;

    @ApiModelProperty(value = "赠送金币" )
    protected Long point;

    @ApiModelProperty(value = "活动优惠" )
    protected java.math.BigDecimal discount;

    @ApiModelProperty(value = "活动优惠" )
    protected java.math.BigDecimal offsetAmount;

    @ApiModelProperty(value = "应退金额" )
    protected java.math.BigDecimal amountPayable;

    @ApiModelProperty(value = "已退金额" )
    protected java.math.BigDecimal amountPaid;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @ApiModelProperty(value = "业务员(employee)")
    @Column(name = "operator")
    protected String operatorId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票")
    protected Integer taxType;

    /**
     *  会员
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Member member;

    @ApiModelProperty(value = "订单明细")
    protected List<AskforItem> itemList = Collections.emptyList();

    public List<AskforItem> sortItemList() {
        List<AskforItem> data = new ArrayList<>();
        data.addAll(getItemList());
        Collections.sort(data, new Comparator<AskforItem>() {
            public int compare(AskforItem n1, AskforItem n2) {

                return n1.getProductId().compareTo(n2.getProductId());
            }
        });
        return data;
    }

    //是否都不需要发货商品
    @JsonIgnore
    public boolean isAllVirtual() {
        for (AskforItem orderItem:getItemList()) {
            if (orderItem.getSubType().equals(0) || orderItem.getSubType().equals(1)) {
                return false;
            }
        }
        return true;
    }

    public String getMemberName() {
        Member member = getMember();
        if (member!=null) {
            return member.getName();
        } else {
            return null;
        }
    }

    public String getMemberMobile() {
        Member member = getMember();
        if (member!=null) {
            return member.getMobile();
        } else {
            return null;
        }
    }

    public BigDecimal calculateSubPrice() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getAmount());

        }
        return amount;
    }

    public BigDecimal calculateSubQuantity() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getQuantity());

        }
        return amount;
    }

    public BigDecimal calculateSubDiscount() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getDiscount());

        }
        return amount;
    }

    public BigDecimal calculateAmountPayable() {
       return calculateSubPrice().subtract(calculateSubDiscount()).add(getFreight()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

    @JsonIgnore
    public String getTitle() {
        for (AskforItem orderItem:getItemList()) {
            return orderItem.getName();
        }
        return null;
    }

    @ApiModelProperty(value = "订单状态")
    public String getStatusDescr() {
        if (getStatus().equals(0) ) {
            return "待确认";
        } else
        if (getStatus().equals(1)) {
            return "已确认";
        } else
        if (getStatus().equals(2)) {
            return "已寄回";
        } else
        if (getStatus().equals(3)) {
            return "待退款";
        } else
        if (getStatus().equals(4)) {
            return "已完成";
        } else
        {
            return "已取消";
        }
    }

    public void setStatusDescr(String value) {

    }

    public AskforItem findAskforItem(Long productId) {
        for (AskforItem askforItem:getItemList()) {
            if (askforItem.getProductId().equals(productId)) {
                return askforItem;
            }
        }
        return null;
    }

}
